﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Module_Command_Detail : DetailPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.initializeWebForm();

        if (!IsPostBack)
        {
            Guid _commandGuid = Guid.Empty;

            if (Request.QueryString["CommandGuid"] != null)
            {
                _commandGuid = new Guid(Request.QueryString["CommandGuid"]);
            }

            Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

            System.Data.DataTable fileTable = moduleProvider.FileGetList();

            commandFileDropDownList.DataSource = fileTable;
            commandFileDropDownList.DataTextField = "FileName";
            commandFileDropDownList.DataValueField = "FileGuid";

            commandFileDropDownList.DataBind();

            if (_commandGuid != Guid.Empty)
            {
                System.Data.DataTable commandTable = moduleProvider.CommandGetByCommandGuid(_commandGuid);

                if (commandTable.Rows.Count == 1)
                {
                    System.Data.DataRow commandRow = commandTable.Rows[0];

                    commandIdTextBox.Text = System.Convert.ToString(commandRow["CommandId"]);
                    commandGuidTextBox.Text = System.Convert.ToString(commandRow["CommandGuid"]);

                    commandNameTextBox.Text = System.Convert.ToString(commandRow["CommandName"]);
                    commandDescriptionTextBox.Text = System.Convert.ToString(commandRow["CommandDescription"]);
                    commandCodeTextBox.Text = System.Convert.ToString(commandRow["CommandCode"]);
                    commandTypeTextBox.Text = System.Convert.ToString(commandRow["CommandType"]);

                    string _fileGuidString = System.Convert.ToString(commandRow["FileGuid"]);
                    foreach (ListItem fileItem in commandFileDropDownList.Items)
                    {
                        if (fileItem.Value == _fileGuidString)
                        {
                            fileItem.Selected = true;
                            break;
                        }
                    }
                }
            }

            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            System.Data.DataTable roleTable = securityProvider.RoleGetList();

            groupDropDownList.DataSource = roleTable;
            groupDropDownList.DataTextField = "RoleName";
            groupDropDownList.DataValueField = "RoleGuid";

            groupDropDownList.DataBind();

            System.Data.DataTable userRoleTable = securityProvider.RoleGetByCommandGuid(_commandGuid);

            groupListBox.DataSource = userRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();

            switch (formMode)
            {
                case FormMode.Create:
                    commandNameTextBox.Enabled = true;
                    commandDescriptionTextBox.Enabled = true;
                    commandCodeTextBox.Enabled = true;
                    commandCodeTextBox.Enabled = true;
                    commandFileDropDownList.Enabled = true;

                    commandNameTextBox.ReadOnly = false;
                    commandDescriptionTextBox.ReadOnly = false;
                    commandCodeTextBox.ReadOnly = false;
                    commandCodeTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Delete:
                    commandNameTextBox.Enabled = true;
                    commandDescriptionTextBox.Enabled = true;
                    commandCodeTextBox.Enabled = true;
                    commandCodeTextBox.Enabled = true;
                    commandFileDropDownList.Enabled = true;

                    commandNameTextBox.ReadOnly = true;
                    commandDescriptionTextBox.ReadOnly = true;
                    commandCodeTextBox.ReadOnly = true;
                    commandCodeTextBox.ReadOnly = true;

                    deleteImageButton.Visible = true;
                    break;
                case FormMode.Edit:
                    commandNameTextBox.Enabled = true;
                    commandDescriptionTextBox.Enabled = true;
                    commandCodeTextBox.Enabled = true;
                    commandCodeTextBox.Enabled = true;
                    commandFileDropDownList.Enabled = true;

                    commandNameTextBox.ReadOnly = false;
                    commandDescriptionTextBox.ReadOnly = false;
                    commandCodeTextBox.ReadOnly = false;
                    commandCodeTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Default:
                    commandNameTextBox.Enabled = true;
                    commandDescriptionTextBox.Enabled = true;
                    commandCodeTextBox.Enabled = true;
                    commandCodeTextBox.Enabled = true;
                    commandFileDropDownList.Enabled = true;

                    commandNameTextBox.ReadOnly = true;
                    commandDescriptionTextBox.ReadOnly = true;
                    commandCodeTextBox.ReadOnly = true;
                    commandCodeTextBox.ReadOnly = true;

                    deleteImageButton.Visible = false;
                    break;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(this.buildForwardUrl("Default.aspx"));
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

        if (formMode == FormMode.Edit)
        {
            if (!moduleProvider.CommandUpdate(
                new Guid(commandGuidTextBox.Text),
                System.Convert.ToInt32(commandIdTextBox.Text),
                new Guid(commandFileDropDownList.SelectedValue),
                commandNameTextBox.Text,
                commandDescriptionTextBox.Text,
                commandCodeTextBox.Text,
                commandTypeTextBox.Text))
            {
                Response.Write("alert(\"An error occured while updating the database\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
        else if (formMode == FormMode.Create)
        {
            if (moduleProvider.CommandInsert(
                Guid.NewGuid(),
                new Guid(commandFileDropDownList.SelectedValue),
                commandNameTextBox.Text,
                commandDescriptionTextBox.Text,
                commandCodeTextBox.Text,
                commandTypeTextBox.Text) == 0)
            {
                Response.Write("alert(\"An error occured while creating database entry\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
    }
    protected void groupListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        UnsubscribeButton.Enabled = groupListBox.SelectedIndex != -1;
    }
    protected void subscribeButton_Click(object sender, EventArgs e)
    {
        Guid _commandGuid = Guid.Empty;

        if (Request.QueryString["CommandGuid"] != null)
        {
            _commandGuid = new Guid(Request.QueryString["CommandGuid"]);
        }

        if (_commandGuid != Guid.Empty)
        {
            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            securityProvider.CommandRoleInsert(_commandGuid, new Guid(groupDropDownList.SelectedValue));

            System.Data.DataTable commandRoleTable = securityProvider.RoleGetByCommandGuid(_commandGuid);

            groupListBox.DataSource = commandRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();
        }
    }
    protected void UnsubscribeButton_Click(object sender, EventArgs e)
    {
        Guid _commandGuid = Guid.Empty;

        if (Request.QueryString["CommandGuid"] != null)
        {
            _commandGuid = new Guid(Request.QueryString["CommandGuid"]);
        }

        if (_commandGuid != Guid.Empty)
        {
            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            securityProvider.CommandRoleDelete(_commandGuid, new Guid(groupListBox.SelectedValue));

            System.Data.DataTable commandRoleTable = securityProvider.RoleGetByCommandGuid(_commandGuid);

            groupListBox.DataSource = commandRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();

            UnsubscribeButton.Enabled = false;
        }
    }
}
